#ifndef CONSOLEWRITERBOX_H_JM_20121207
#define CONSOLEWRITERBOX_H_JM_20121207

// ## jm4bbx
#include "StreamWriterBox.h"
#include "../../common_bbx/mutex/mutex.h"
#include "../../LogConf.h"

// ## c++
#include <algorithm>
#include <vector>
#include <iostream>

namespace jm4bbx {

	template<class OutputBoxType>
	class ConsoleWriterBox : public OutputBoxType {
		
	public:
		struct ConsoleOutputStream : public StreamWriterBox::OutputStream {
			JM_USE_LOGGER(ResultLogger)
			virtual bool open() { return true; };
			virtual bool close() { 	return true; };

			virtual ConsoleOutputStream& operator<<(const std::string& str) {
				{
					// Nie je treba zamykat pretoze logger je synchronizovany
					// v pripade merania casu resp. v release je logger vypnuty
					// takze sa ani nic zbytocne nevypisuje.
					Lock<Mutex>::ScopeLock lock(mutex_);
					std::cout << str;
					//size_t pos = str.find_last_of("\n");
					//if (pos != std::string::npos &&
					//	pos >= str.size() - 2) {
					//		LOG_T(str.substr(0, pos));		
					//}
					//else {
					//	LOG_T(str);		
					//}

				}
				return *this;
			}

		private:
			mutable Mutex mutex_;
		
		};


		ConsoleWriterBox(const BoxParamPack& p) : OutputBoxType(p) { }

		virtual typename OutputBoxType::OutputStream& getOutputStream() override { 
			return cout_;
		}
	private:
		static ConsoleOutputStream cout_;
	};

	template<class OutputBoxType>
	typename ConsoleWriterBox<OutputBoxType>::ConsoleOutputStream ConsoleWriterBox<OutputBoxType>::cout_;


/*	template<class T>
	ConsoleWriterBox<T>::ConsoleOutputStream ConsoleWriterBox<T>::cout_;*/
} // namespace jm4bbx

#endif
